//先加载页面完成
$(function () {
    numerationFn(null, $(".process"));

    /**
     * 步骤编号以及逻辑判断
     */
    function numerationFn(num, obj) {
        var steps = [];
        steps = obj.children(".dmSeq").children();
        for (var i = 0; i < steps.length; i++) {
            var html = ``;
            html = `<p class="serialNumber">${i + 1}</p>`
            if (num != null) {
                html = `<p class="serialNumber">${num}.${i + 1}</p>`
            }
            $(steps[i]).prepend(html);
            //适用性引用
            var applic = $(steps[i]).attr("applicrefid");
            if (typeof (applic) != "undefined") {
                var applicHtml = $("#" + applic).html();
                $(steps[i]).append(`<div class="applic">
                    <p class="serialNumber" style="width: 80px">ApplicRef</p>
                    ${applicHtml}
                </div>`);
            }
            // 不进入dmif提示
            if ($(steps[i]).hasClass("dmIf")&&$(steps[i]).children().hasClass("rhombus")) {
                $(steps[i]).addClass("positionrelative")
                $(steps[i]).prepend(`<div class="singleArrow"><p class="falseFont">false</p></div>`)
                $(steps[i]).prepend(`<p class="stepFont">前往步骤${i + 2}</p>`)
                $(steps[i]).children(".expression").addClass("expressionFont");
                //有循环体
                if($(steps[i]).children().hasClass("dmThenSeq")){
                    $(steps[i]).prepend(`<div class="heightArrow"></div>`);
                    $(steps[i]).children(".dmThenSeq").css("marginTop","80px");
                    var elDmSeq = $(steps[i]).children(".dmThenSeq").children(".dmLoop").children(".dmSeq");
                    if ($(elDmSeq).length>0&&$(elDmSeq).find(".dmNode[applicrefid='wrongAnswer']").length>0) {
                        $(steps[i]).prepend('<div class="heightRightArrow" id="eleTop"></div>');
                        var eleTopValue = $(elDmSeq).find(".dmNode[applicrefid='wrongAnswer']").position().top;
                        $("#eleTop").height(eleTopValue-60);
                    }
                }
            }
            if ($(steps[i]).find(".dmSeq").length > 0) {
                numerationFn(i + 1, $(steps[i]).find(".dmSeq").parent());
            }


        }
    }

    /**
     * assertion赋值
     * @type {*[]}
     */
    var assertion = [];
    assertion = $(".assertion");
    assertion.each(function () {
        $(this).children(".variableRef").after(" = ");
    });
    $(".assertion").children().css("display", "inline-block");
    $(".assertion").css("display", "inline-block");

    $(".userEntry,.menuChoice").children(".prompt").css("display", "inline-block");
    $(".dmLoop").children(".expression").css({"border": "2px dashed", "width": "90%", "margin": "10px auto","display":"block"})
    $(".dmLoop").children(".expression").prepend(`<h5>循环进入条件</h5>`)

    // 通过变量名，监听变量值，获取表达式的值与进入循环的表达式对比，判断跳出出循环的表达式
    var intoLoopEx = expressionFn($(".dmLoop").children(".expression"));// tourFinished=false
    console.log("--进入循环条件表达式--" + intoLoopEx); // tourFinished=false&&tourFinished=false
    // 获取表达式中操作的变量
    var variableName = "";
    var separator = ["=", ">", "<", ">=", "<="];
    for (let i = 0; i < separator.length - 1; i++) {
        if (intoLoopEx.indexOf(separator[i]) > 0) {
            variableName = intoLoopEx.split(separator[i])[0];
        }
    }
    var ref = getvariableRef(variableName);
    console.log("--getvariableRef--" + ref);
    if (ref[0] != intoLoopEx) {
        $(ref[1]).parent().parent().append(`<span style="border: 1px dashed;">循环结束,退出循环</span>`)
    }

    //表达式拼接函数
    function expressionFn(element) {
        var operatorValue1 = "";
        var numberOperation = "";
        var operatorValue2 = "";
        if ($(element).children(".expression").length > 0) {
            // 多个表达式
            //表达式运算符
            if ($(element).children().hasClass("numberOperation")) {
                numberOperation = symbolicFn($(element).children(".numberOperation").text());
            }
            console.log("--numberOperation--" + numberOperation);
            operatorValue1 = expressionFn($(element).children(".expression")[0]);
            console.log("--operatorValue1--" + operatorValue1);
            operatorValue2 = expressionFn($(element).children(".expression")[1]);
            console.log("--operatorValue2--" + operatorValue2);
            //拼接整个表达式
            var operatorList = operatorValue1 + numberOperation + operatorValue2;
            return operatorList;
        } else {
            // 单个表达式取值
            if ($(element).children().hasClass("variableRef")) {
                loopVariableName = $(element).children(".variableRef").text();
                return $(element).children(".variableRef").text();
            } else {
                return $(element).children().text();
            }
        }

    };

    // 符号函数
    function symbolicFn(opt) {
        var operator = opt;
        switch (operator) {
            case "and":
                operator = "&&";
                break;
            case "or":
                operator = "||";
                break;
            case "not":
                operator = "!";
                break;
            case "+":
                operator = "+";
                break;
            case "=":
                operator = "=";
                break;
        }
        return operator;
    };

    // 获取变量被修改方法，value修改的变量名
    function getvariableRef(value) {
        var list = $(".dmLoop").children(".dmSeq").find(".variableRef");
        var setVariableRef = "";
        var obj = null;
        list.each(function () {
            if ($(this).text().trim() == value) {
                if ($(this).parent().hasClass("assertion")) {
                    setVariableRef = value + "=" + expressionFn($(this).parent().children(".expression"));
                } else if ($(this).parent().hasClass("expression")) {
                    setVariableRef = expressionFn($(this).parent().parent());
                }
                obj = $(this);
            }
        })
        return [setVariableRef, obj];
    }

});


